package com.github.caijh.mall.category.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.caijh.commons.util.Asserts;
import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.core.exception.BizException;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.category.model.Category;
import com.github.caijh.mall.category.request.CategoryReqBody;
import com.github.caijh.mall.category.request.CategorySearchReqBody;
import com.github.caijh.mall.category.service.CategoryService;
import com.github.caijh.mall.category.vo.CategoryTreeVo;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.system.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/admin/category")
@Api(tags = "分类服务")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    /**
     * 分页
     *
     * @param reqBody CategorySearchReqBody
     * @return page
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<Category>> page(@ModelAttribute CategorySearchReqBody reqBody) {
        IPage<Category> page = categoryService.findPage(reqBody);
        return CommonResult.success(CommonPage.restPage(page.getRecords()));
    }

    /**
     * 新增分类
     *
     * @param reqBody RequestBody
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public void save(@Validated CategoryReqBody reqBody) {
        if (categoryService.checkName(reqBody.getName(), reqBody.getType()) > 0) {
            throw BizException.of("CATEGORY_NAME_EXIST");
        }

        Category category = new Category();
        BeanUtils.copyProperties(reqBody, category);
        category.setPath(categoryService.getPathByPId(category.getPid()));
        category.setExtra(systemAttachmentService.clearPrefix(category.getExtra()));
        categoryService.save(category);
    }

    /**
     * 删除分类表
     *
     * @param id Integer
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    @ApiImplicitParam(name = "id", value = "分类ID")
    public void delete(@RequestParam(value = "id") Integer id) {
        categoryService.delete(id);
    }

    /**
     * 修改分类
     *
     * @param id              integer id
     * @param categoryReqBody 修改参数
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    @ApiImplicitParam(name = "id", value = "分类ID")
    public void update(@RequestParam Integer id, @RequestBody CategoryReqBody categoryReqBody) {
        Asserts.isTrue(id != null && id > 0, () -> BizException.of("CATEGORY_ID_NULL"));

        categoryReqBody.setExtra(systemAttachmentService.clearPrefix(categoryReqBody.getExtra()));
        categoryService.update(categoryReqBody, id);
    }

    /**
     * 查询分类表信息
     *
     * @param id Integer
     * @return Category
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    @ApiImplicitParam(name = "id", value = "分类ID")
    public Category info(@RequestParam(value = "id") Integer id) {
        return categoryService.getById(id);
    }


    /**
     * 获取分类树.
     *
     * @param type   分类类型
     * @param status 状态
     * @param name   搜索分类名称
     * @return List of CategoryTreeVo
     */
    @ApiOperation(value = "获取tree结构的列表")
    @GetMapping(value = "/list/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型ID | 类型，1 产品分类，2 附件分类，3 文章分类， 4 设置分类， 5 菜单分类， 6 配置分类， 7 秒杀配置", example = "1"),
            @ApiImplicitParam(name = "status", value = "-1=全部，0=未生效，1=已生效", example = "1"),
            @ApiImplicitParam(name = "name", value = "模糊搜索", example = "电视")
    })
    public List<CategoryTreeVo> getListTree(@RequestParam(name = "type") Integer type,
                                            @RequestParam(name = "status") Integer status,
                                            @RequestParam(name = "name", required = false) String name) {
        return categoryService.getTree(type, status, name);
    }

    /**
     * 根据分类id集合获取分类数据
     *
     * @param ids String id集合字符串
     * @return list of Category
     */
    @ApiOperation(value = "根据id集合获取分类列表")
    @GetMapping(value = "/list/ids")
    @ApiImplicitParam(name = "ids", value = "分类id集合")
    public List<Category> getByIds(@Validated @RequestParam(name = "ids") String ids) {
        return categoryService.getByIds(Strings.toIntListByComma(ids));
    }

    /**
     * 更改分类状态
     *
     * @param id Integer 分类id
     */
    @ApiOperation(value = "更改分类状态")
    @GetMapping(value = "/updateStatus/{id}")
    @ApiImplicitParam(name = "id", value = "分类id")
    public void getByIds(@Validated @PathVariable(name = "id") Integer id) {
        categoryService.updateStatus(id);
    }

}



